package com.ewhome.apartment.mvp.presenter;

import android.app.Application;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.ColumnLayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.ewhome.apartment.app.utils.RxUtils;
import com.ewhome.apartment.mvp.model.entity.SectionData;
import com.ewhome.apartment.mvp.model.entity.res.ResHomeBean;
import com.ewhome.apartment.mvp.ui.adapter.BannerAdapter;
import com.ewhome.apartment.mvp.ui.adapter.LocationTitAdapter;
import com.ewhome.apartment.mvp.ui.adapter.NewsAdapter;
import com.ewhome.apartment.mvp.ui.adapter.RecylerApartmentAdapter;
import com.ewhome.apartment.mvp.ui.adapter.RecylerViewAdapter;
import com.ewhome.apartment.mvp.ui.adapter.SectionAdapter;
import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewhome.apartment.mvp.contract.HomeContract;
import com.jess.arms.utils.ArmsUtils;

import java.util.LinkedList;
import java.util.List;


@ActivityScope
public class HomePresenter extends BasePresenter<HomeContract.Model, HomeContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private Gson mGson;
    private RecylerApartmentAdapter recylerApartmentAdapter;
    private RecylerApartmentAdapter recylerPartApartmentAdapter;
    @Inject
    public HomePresenter(HomeContract.Model model, HomeContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager, Gson mGson) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
        this.mGson=mGson;
    }
    public void getHomedata(){
        mModel.getHomedata().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResHomeBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResHomeBean resHomeBean) {
                        recylerApartmentAdapter.updatedata(resHomeBean.getItems());
                        recylerPartApartmentAdapter.updatedata(resHomeBean.getItems());
//                        ArmsUtils.makeText(mRootView.getcurrentContext(),mGson.toJson(resHomeBean));
                    }
                });
    }

    public void initDelegeAdapter(DelegateAdapter adapter){
//        adapter.addAdapter();
        SingleLayoutHelper singleLayoutHelper=new SingleLayoutHelper();
//       adapter.addAdapter(new LocationTitAdapter(mRootView.getcurrentContext(),singleLayoutHelper));

       adapter.addAdapter(new BannerAdapter(mRootView.getcurrentContext(),singleLayoutHelper));
       adapter.addAdapter(new SectionAdapter(mRootView.getcurrentContext(),singleLayoutHelper,new SectionData("觅窝情报站","最好最快的一手觅窝资讯")));
       SingleLayoutHelper newshelper=new SingleLayoutHelper();
        newshelper.setMarginBottom(10);
       adapter.addAdapter(new RecylerViewAdapter(mRootView.getcurrentContext(),newshelper));
       adapter.addAdapter(new SectionAdapter(mRootView.getcurrentContext(),singleLayoutHelper,new SectionData("公寓整租","就是这个味")));
       recylerApartmentAdapter=new RecylerApartmentAdapter(mRootView.getcurrentContext(),singleLayoutHelper);
       adapter.addAdapter(recylerApartmentAdapter);
        adapter.addAdapter(new SectionAdapter(mRootView.getcurrentContext(),singleLayoutHelper,new SectionData("公寓合租","合租一个女朋友")));
//       adapter.addAdapter(new );
        recylerPartApartmentAdapter=new RecylerApartmentAdapter(mRootView.getcurrentContext(),singleLayoutHelper);
        adapter.addAdapter(recylerPartApartmentAdapter);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

}
